/*
 * DialogThread.java
 *
 * Created on 2.12.2007, 15:06:03
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.threads;

import java.util.Date;

import cz.possoft.client.gui.dialog.SimpleDialog;
import cz.possoft.client.gui.dialog.VyzivaciDialog;
import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;

/**
 *
 * @author mara
 *
 * vlakno kontroluje dialogova okna
 * nejake bylo otevreno.
 */
public class DialogThread extends Thread {

    static int sec;
    static int min;

    public DialogThread() {
        setPriority(1);
        start();
    }


    // metoda nakonec neni pouzivana. Modalni dialogy se resi v metode windowActivated ve tride ShowApplet.    
    public void run() {
        try {
            while (true) {
                //try {
                // cekame 5 sekund pred kazdym spustenim kontroly socketu a vsech dialogu
                sleep(5000);
                if ((NastaveniShowPanel.getConnectThread() != null) && (NastaveniShowPanel.getConnectThread().getClientSocket().isClosed())) {
                    System.out.println("KONTROLA: CLOSED");
                } else {
                    System.out.println("KONTROLA: NOT_CLOSED");
//                    Socket clientSocket = new Socket(InetAddress.getByName(Text.HOST), Text.PORT);
                    //                  clientSocket.close();
                    //controlVyzivaciDialog();
                    //controlSimpleDialog();
                    //controlPesecNaKonciDialog();
//                } catch (UnknownHostException e) {
//                    exceptionCatch();
//                } catch (IOException ie) {
//                    exceptionCatch();
//                }
                }
            }
        } catch (InterruptedException ie) {
            System.err.println("Interr. exception v DialogChecker vlaknu.");
        }
    }

    
    // metoda kvuli nestandardnimu chovani socketu pri padu serveru na aise.
    // Prozatim neni v aplikaci pouzita
    public void exceptionCatch() {
        ConnectThread ct = NastaveniShowPanel.getConnectThread();
        if (ct != null) {
            ct.dispose();
            try {
                ct.destroy();
            } catch (Exception e) {
                System.err.println("Destroy exception catched in DialogThread.exceptionCatch.");
            }
            NastaveniShowPanel.setConnectThread(new ConnectThread());
        }
    }

    // vyvolano pri aktivaci appletu z metody ShowApplet.windowClosing
    public static void controlDialogs() {
        DialogThread.controlVyzivaciDialog();
        DialogThread.controlSimpleDialog();
        //DialogThread.controlPesecNaKonciDialog();
    }
    
    private static void controlVyzivaciDialog() {
        // kontrola casu kvuli tomu, ze se nekdy zobrazilo spousta oken a pretekl zasobnik.
        Date d = new Date();
        int seconds = d.getSeconds();
        int minutes = d.getMinutes();
        if ((seconds != sec) && (minutes != min)) {
            sec = seconds;
            min = minutes;
        VyzivaciDialog dialog = VyzivaciDialog.getCurrentInstance();
        if (dialog != null) {
            int context = dialog.getContext(); 
            dialog.dispose();
            dialog = VyzivaciDialog.getInstance(NastaveniShowPanel.getConnectThread().getOpponentName(), NastaveniShowPanel.getConnectThread().getGameKey(), context);
        }
        }
    }

    
    private static void controlSimpleDialog() {
        // kontrola casu kvuli tomu, ze se nekdy zobrazilo spousta oken a pretekl zasobnik.
        Date d = new Date();
        int seconds = d.getSeconds();
        int minutes = d.getMinutes();
        if ((seconds != sec) && (minutes != min)) {
            sec = seconds;
            min = minutes;
            //System.out.println("CONTROL SIMPLE DIALOG ");
            SimpleDialog dialog = SimpleDialog.getCurrentInstance();
            if (dialog != null) {//&& (dialog.getShown()) && (dialog.currentInstanceAsked)) {
                // && (VyzivaciDialog.isPomPlayerNullFlag() == false)) {
                String title = dialog.getTitle();
                String message = dialog.getMessage();
                //dialog.setShown(false);
                dialog.dispose();
                dialog = SimpleDialog.getInstance(title, message);
                //dialog.setShown(true);
            }
        }
    }

//    private static void controlPesecNaKonciDialog() {
//        // kontrola casu kvuli tomu, ze se nekdy zobrazilo spousta oken a pretekl zasobnik.
//        Date d = new Date();
//        int seconds = d.getSeconds();
//        int minutes = d.getMinutes();
//        if ((seconds != sec) && (minutes != min)) {
//            sec = seconds;
//            min = minutes;
//        PesecNaKonciDialog dialog = PesecNaKonciDialog.getCurrentInstance();
//        if (dialog != null) {
//            int color = dialog.getColor();
//            int xx = dialog.getXx();
//            int yy = dialog.getYy();
//
//            dialog.dispose();
//            dialog = PesecNaKonciDialog.getInstance(color, xx, yy);
//        }
//        }
//    }
}
